package com.oceansoft.mobile.econsole.modules.base.entity;

import com.oceansoft.mobile.econsole.common.constant.Constant;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/2/3.
 */
public class WxQuery {

        public final static int PAGE_SIZE = 10;
        private int numPerPage = PAGE_SIZE;
        private int pageNum = 1;
        private String orderField;
        private String orderDirection;
        private String keywords;
        private Date start;
        private Date end;
        private String handler;
        private String accidentId;
        private String userSrc;
        private String content_number;
        private String sort;
        private String loginId;
        private int APIId;//监控接口
        private int receiver;//推送对象
        private int djstatus;//快撤理赔单据状态
        private String qtype;
        public static final String ASC = "ASC";
        private String order = ASC;
        private String query;
        private int page = 1;
        private int rows = Constant.PAGE_SIZE;
        public static final String COUNT_SQL = "count_sql";

        public static String getCountSql() {
            return COUNT_SQL;
        }

        public int getPage() {
            return page;
        }

        public void setPage(int page) {
            this.page = page;
        }

        public int getRows() {
            return rows;
        }

        public void setRows(int rows) {
            this.rows = rows;
        }

        public static final String PAGE_SQL = "page_sql";

        public String getOrder() {
            return order;
        }

        public void setOrder(String order) {
            this.order = order;
        }

        public String getQuery() {
            return query;
        }

        public void setQuery(String query) {
            this.query = query;
        }

        public String getQtype() {
            return qtype;
        }

        public void setQtype(String qtype) {
            this.qtype = qtype;
        }

        public int getAPIId() {
            return APIId;
        }

        public void setAPIId(int aPIId) {
            APIId = aPIId;
        }

        public String getLoginId() {
            return loginId;
        }

        public void setLoginId(String loginId) {
            this.loginId = loginId;
        }

        public String getSort() {
            return sort;
        }

        public void setSort(String sort) {
            this.sort = sort;
        }

        public String getContent_number() {
            return content_number;
        }

        public void setContent_number(String content_number) {
            this.content_number = content_number;
        }

        public String getUserSrc() {
            return userSrc;
        }

        public void setUserSrc(String userSrc) {
            this.userSrc = userSrc;
        }

        public String getHandler() {
            return handler;
        }

        public void setHandler(String handler) {
            this.handler = handler;
        }

        public String getAccidentId() {
            return accidentId;
        }

        public void setAccidentId(String accidentId) {
            this.accidentId = accidentId;
        }

        public WxQuery() {
        }

        public WxQuery(int pageSize, int pageIndex) {
            this.numPerPage = pageSize;
            this.pageNum = pageIndex;
        }

        public int getNumPerPage() {
            return numPerPage;
        }

        public void setNumPerPage(int numPerPage) {
            this.numPerPage = numPerPage;
        }

        public int getPageNum() {
            return this.pageNum > 0 ? this.pageNum : PAGE_SIZE;
        }

        public void setPageNum(int pageNum) {
            this.pageNum = pageNum;
        }

        public String getOrderField() {
            return orderField;
        }

        public void setOrderField(String orderField) {
            this.orderField = orderField;
        }

        public String getOrderDirection() {
            return "desc".equals(orderDirection) ? "desc" : "asc";
        }

        public void setOrderDirection(String orderDirection) {
            this.orderDirection = orderDirection;
        }

        public String getKeywords() {
            return keywords;
        }

        public void setKeywords(String keywords) {
            if (null != keywords && keywords.trim().length() > 0)
                this.keywords = keywords;
        }

        public int getStartRow() {
            if (1 >= pageNum) {
                return 0;
            } else {
                return (pageNum - 1) * numPerPage;
            }
        }

        public int getEndRow() {
            return numPerPage;
        }

        public Date getStart() {
            return start;
        }

        public void setStart(Date start) {
            this.start = start;
        }

        public Date getEnd() {
            return end;
        }

        public void setEnd(Date end) {
            this.end = end;
        }

        public int getReceiver() {
            return receiver;
        }

        public void setReceiver(int receiver) {
            this.receiver = receiver;
        }

        public int getDjstatus() {
            return djstatus;
        }

        public void setDjstatus(int djstatus) {
            this.djstatus = djstatus;
        }


        public static String getPageSQL(WxQuery query) {
            return getSQL(null, query).get(PAGE_SQL);
        }
        public static String getCountSQL(WxQuery query) {
            return getSQL(null, query).get(COUNT_SQL);
        }

        public static String getCountSQL(String tablPrefix, WxQuery query) {
            return getSQL(tablPrefix, query).get(COUNT_SQL);
        }

        public static String getPageSQL(String tablPrefix, WxQuery query) {
            return getSQL(tablPrefix, query).get(PAGE_SQL);
        }
        public static Map<String, String> getSQL(String tablPrefix, WxQuery query) {
            final StringBuilder page_sql = new StringBuilder();
            final StringBuilder count_sql = new StringBuilder();
            String tabPre = StringUtils.hasText(tablPrefix) ? (tablPrefix + ".") : tablPrefix;
            //like
            if (StringUtils.hasText(query.getQtype()) && StringUtils.hasText(query.getQuery()) && !"-1".equals(query.getQuery())) {
                page_sql.append(" AND ").append(tabPre).append(query.getQtype().toUpperCase()).append(" LIKE('%").append(query.getQuery()).append("%')");
                count_sql.append(" AND ").append(tabPre).append(query.getQtype().toUpperCase()).append(" LIKE('%").append(query.getQuery()).append("%')");
            }
            //order TODO:注意防范SQL注入
            if (StringUtils.hasText(query.getSort())) {
                page_sql.append(" ORDER BY ").append(tabPre).append(query.getSort().toUpperCase()).append(" ");
                page_sql.append(StringUtils.hasText(query.getOrder()) ? query.getOrder().toUpperCase() : WxQuery.ASC);
            }
            //分页
            int _size = 0 != query.getRows() ? query.getRows() : Constant.PAGE_SIZE;
            int _start = 2 > _size ? 0 : _size * (query.getPage() - 1);
            page_sql.append(String.format(" LIMIT %1$d,%2$d", _start, _size));
            return new HashMap<String, String>() {{
                put(PAGE_SQL, page_sql.toString());
                put(COUNT_SQL, count_sql.toString());
            }};
        }

}
